<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Setting Polices at Namespace Scope</title>
<link rel="stylesheet" href="../../math.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Math Toolkit 4.2.1">
<link rel="up" href="../pol_ref.html" title="Policy Reference">
<link rel="prev" href="policy_defaults.html" title="Using Macros to Change the Policy Defaults">
<link rel="next" href="pol_ref_ref.html" title="Policy Class Reference">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="policy_defaults.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../pol_ref.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="pol_ref_ref.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="math_toolkit.pol_ref.namespace_pol"></a><a class="link" href="namespace_pol.html" title="Setting Polices at Namespace Scope">Setting Polices at
      Namespace Scope</a>
</h3></div></div></div>
<p>
        Sometimes what you really want to do is bring all the special functions,
        or all the distributions into a specific namespace-scope, along with a specific
        policy to use with them. There are two macros defined to assist with that:
      </p>
<pre class="programlisting"><span class="identifier">BOOST_MATH_DECLARE_SPECIAL_FUNCTIONS</span><span class="special">(</span><span class="identifier">Policy</span><span class="special">)</span>
</pre>
<p>
        and:
      </p>
<pre class="programlisting"><span class="identifier">BOOST_MATH_DECLARE_DISTRIBUTIONS</span><span class="special">(</span><span class="identifier">Type</span><span class="special">,</span> <span class="identifier">Policy</span><span class="special">)</span>
</pre>
<p>
        You can use either of these macros after including any special function or
        distribution header. For example:
      </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">special_functions</span><span class="special">/</span><span class="identifier">gamma</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="comment">//using boost::math::tgamma;</span>
<span class="comment">// Need not declare using boost::math::tgamma here,</span>
<span class="comment">// because will define tgamma in myspace using macro below.</span>

<span class="keyword">namespace</span> <span class="identifier">myspace</span>
<span class="special">{</span>
  <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">;</span>

  <span class="comment">// Define a policy that does not throw on overflow:</span>
  <span class="keyword">typedef</span> <span class="identifier">policy</span><span class="special">&lt;</span><span class="identifier">overflow_error</span><span class="special">&lt;</span><span class="identifier">errno_on_error</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">my_policy</span><span class="special">;</span>

  <span class="comment">// Define the special functions in this scope to use the policy:   </span>
  <span class="identifier">BOOST_MATH_DECLARE_SPECIAL_FUNCTIONS</span><span class="special">(</span><span class="identifier">my_policy</span><span class="special">)</span>
<span class="special">}</span>

<span class="comment">// Now we can use myspace::tgamma etc.</span>
<span class="comment">// They will automatically use "my_policy":</span>
<span class="comment">//</span>
<span class="keyword">double</span> <span class="identifier">t</span> <span class="special">=</span> <span class="identifier">myspace</span><span class="special">::</span><span class="identifier">tgamma</span><span class="special">(</span><span class="number">30.0</span><span class="special">);</span> <span class="comment">// Will *not* throw on overflow,</span>
<span class="comment">// despite the large value of factorial 30 = 265252859812191058636308480000000</span>
<span class="comment">// unlike default policy boost::math::tgamma;</span>
</pre>
<p>
        In this example, using BOOST_MATH_DECLARE_SPECIAL_FUNCTIONS results in a
        set of thin inline forwarding functions being defined:
      </p>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="keyword">inline</span> <span class="identifier">T</span> <span class="identifier">tgamma</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">a</span><span class="special">){</span> <span class="keyword">return</span> <span class="special">::</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">tgamma</span><span class="special">(</span><span class="identifier">a</span><span class="special">,</span> <span class="identifier">mypolicy</span><span class="special">());</span> <span class="special">}</span>

<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="keyword">inline</span> <span class="identifier">T</span> <span class="identifier">lgamma</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">a</span><span class="special">)</span> <span class="special">(</span> <span class="keyword">return</span> <span class="special">::</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">lgamma</span><span class="special">(</span><span class="identifier">a</span><span class="special">,</span> <span class="identifier">mypolicy</span><span class="special">());</span> <span class="special">}</span>
</pre>
<p>
        and so on. Note that while a forwarding function is defined for all the special
        functions, however, unless you include the specific header for the special
        function you use (or boost/math/special_functions.hpp to include everything),
        you will get linker errors from functions that are forward declared, but
        not defined.
      </p>
<p>
        We can do the same thing with the distributions, but this time we need to
        specify the floating-point type to use:
      </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">distributions</span><span class="special">/</span><span class="identifier">cauchy</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">namespace</span> <span class="identifier">myspace</span>
<span class="special">{</span> <span class="comment">// using namespace boost::math::policies; // May be convenient in myspace.</span>

  <span class="comment">// Define a policy called my_policy to use.</span>
  <span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">::</span><span class="identifier">policy</span><span class="special">;</span>

<span class="comment">// In this case we want all the distribution accessor functions to compile,</span>
<span class="comment">// even if they are mathematically undefined, so</span>
<span class="comment">// make the policy assert_undefined.</span>
  <span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">::</span><span class="identifier">assert_undefined</span><span class="special">;</span>

<span class="keyword">typedef</span> <span class="identifier">policy</span><span class="special">&lt;</span><span class="identifier">assert_undefined</span><span class="special">&lt;</span><span class="keyword">false</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">my_policy</span><span class="special">;</span>

<span class="comment">// Finally apply this policy to type double.</span>
<span class="identifier">BOOST_MATH_DECLARE_DISTRIBUTIONS</span><span class="special">(</span><span class="keyword">double</span><span class="special">,</span> <span class="identifier">my_policy</span><span class="special">)</span>
<span class="special">}</span> <span class="comment">// namespace myspace</span>

<span class="comment">// Now we can use myspace::cauchy etc, which will use policy</span>
<span class="comment">// myspace::mypolicy:</span>
<span class="comment">//</span>
<span class="comment">// This compiles but throws a domain error exception at runtime.</span>
<span class="comment">// Caution! If you omit the try'n'catch blocks, </span>
<span class="comment">// it will just silently terminate, giving no clues as to why! </span>
<span class="comment">// So try'n'catch blocks are very strongly recommended.</span>

<span class="keyword">void</span> <span class="identifier">test_cauchy</span><span class="special">()</span>
<span class="special">{</span>
   <span class="keyword">try</span>
   <span class="special">{</span>
      <span class="keyword">double</span> <span class="identifier">d</span> <span class="special">=</span> <span class="identifier">mean</span><span class="special">(</span><span class="identifier">myspace</span><span class="special">::</span><span class="identifier">cauchy</span><span class="special">());</span>  <span class="comment">// Cauchy does not have a mean!</span>
      <span class="special">(</span><span class="keyword">void</span><span class="special">)</span> <span class="identifier">d</span><span class="special">;</span>
   <span class="special">}</span>
   <span class="keyword">catch</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">domain_error</span><span class="special">&amp;</span> <span class="identifier">e</span><span class="special">)</span>
   <span class="special">{</span>
      <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">e</span><span class="special">.</span><span class="identifier">what</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
   <span class="special">}</span>
<span class="special">}</span>
</pre>
<p>
        In this example the result of BOOST_MATH_DECLARE_DISTRIBUTIONS is to declare
        a typedef for each distribution like this:
      </p>
<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">cauchy_distribution</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">,</span> <span class="identifier">my_policy</span><span class="special">&gt;</span> <span class="identifier">cauchy</span><span class="special">;</span>
<span class="identifier">tyepdef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">gamma_distribution</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">,</span> <span class="identifier">my_policy</span><span class="special">&gt;</span> <span class="identifier">gamma</span><span class="special">;</span>
</pre>
<p>
        and so on. The name given to each typedef is the name of the distribution
        with the "_distribution" suffix removed.
      </p>
</div>
<div class="copyright-footer">Copyright © 2006-2021 Nikhar Agrawal, Anton Bikineev, Matthew Borland,
      Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert Holin, Bruno
      Lalande, John Maddock, Evan Miller, Jeremy Murphy, Matthew Pulver, Johan Råde,
      Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg, Daryle
      Walker and Xiaogang Zhang<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="policy_defaults.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../pol_ref.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="pol_ref_ref.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
